package com.ruibang.glass.sale.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.sale.constant.Constants;
import com.ruibang.glass.sale.domain.req.GSaleOrderReq;
import com.ruibang.glass.sale.entity.GSaleContractManagement;
import com.ruibang.glass.sale.entity.GSaleOrderManagement;
import com.ruibang.glass.sale.feign.MaterialApi;
import com.ruibang.glass.sale.service.GSaleOrderManagementService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author mameng
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/api/glass/sale/order/management")
public class GSaleOrderManagementController {
    @Autowired
    private GSaleOrderManagementService gSaleOrderManagementService;

    @Autowired
    private MaterialApi materialApi;

    @PostMapping("list")
    @ApiOperation(value = "订单管理列表", response = ResultBody.class)
    public ResultBody list(@RequestBody GSaleOrderReq gSaleOrderReq) {
        return ResultBody.success(gSaleOrderManagementService.getPage(gSaleOrderReq));
    }

    /**
     * 选择合同后只能使用合同里面的产品
     */
    @GetMapping("/getContractNumber")
    @ApiOperation(value = "获取订单编号", response = ResultBody.class)
    public ResultBody getContractNumber() {
        return ResultBody.success(gSaleOrderManagementService.generateNextSerialNumber());
    }

    @PostMapping("add")
    @ApiOperation(value = "增加", response = ResultBody.class)
    public ResultBody addGSaleOrderManagement(@RequestBody GSaleOrderManagement gSaleOrderManagement) {
        gSaleOrderManagementService.addGSaleOrderManagement(gSaleOrderManagement);
        return ResultBody.success();
    }


    @GetMapping("/processNotice/{businessId}")
    @ApiOperation(value = "流程处理通知", response = ResultBody.class)
    public ResultBody processNotice(@PathVariable("businessId") String businessId) {
        gSaleOrderManagementService.processNotice(businessId);
        return ResultBody.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改", response = ResultBody.class)
    public ResultBody updateGSaleOrderManagement(@RequestBody GSaleOrderManagement gSaleOrderManagement) {
        gSaleOrderManagementService.updateGSaleOrderManagement(gSaleOrderManagement);
        return ResultBody.success();
    }

    @PostMapping("removeByIdOrIds")
    @ApiOperation(value = "删除", response = ResultBody.class)
    public ResultBody removeByIdOrIds(@RequestBody GSaleOrderManagement gSaleOrderManagement) {
        return ResultBody.success(gSaleOrderManagementService.removeBatchByIds(gSaleOrderManagement.getIds()));
    }

    @GetMapping("getOne/{id}")
    @ApiOperation(value = "查询", response = ResultBody.class)
    public ResultBody getGSaleOrderManagement(@PathVariable("id") String id) {
        GSaleOrderManagement gSaleContractManagementById = gSaleOrderManagementService.getGSaleOrderManagement(id);
        return ResultBody.success(gSaleContractManagementById);
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "查看详情", response = ResultBody.class)
    public ResultBody getById(@PathVariable("id") String id) {
        return ResultBody.success(gSaleOrderManagementService.getGSaleOrderManagement(id));
    }

    @GetMapping("getMaterialList")
    @ApiOperation(value = "商品下拉", response = ResultBody.class)
    public ResultBody list() {
        return materialApi.getMaterialSelect();
    }

    @GetMapping("getOrderSelectList")
    @ApiOperation(value = "订单下拉", response = ResultBody.class)
    public ResultBody getOrderSelectList() {
        return ResultBody.success(gSaleOrderManagementService.list(Wrappers
                .<GSaleOrderManagement>lambdaQuery().select(GSaleOrderManagement::getId,
                        GSaleOrderManagement::getOrderNumber)
                .eq(GSaleOrderManagement::getApproveStatus, Constants.TWO_STR)
                )
        );
    }


    @GetMapping("getOrderByCustomerIdList/{id}")
    @ApiOperation(value = "根据客户Id获取订单下拉", response = ResultBody.class)
    public ResultBody list(@PathVariable String id) {
        return ResultBody.success(gSaleOrderManagementService.list(Wrappers
                .<GSaleOrderManagement>lambdaQuery().select(GSaleOrderManagement::getId,
                        GSaleOrderManagement::getOrderNumber).eq(GSaleOrderManagement::getConsignorSettlementCustomer, id)));
    }


    @PostMapping("updateGSaleOrderManagementByNumber")
    @ApiOperation(value = "修改发货数量", response = ResultBody.class)
    public ResultBody updateGSaleOrderManagementByNumber(@RequestBody GSaleOrderManagement gSaleOrderManagement) {
        gSaleOrderManagementService.updateGSaleOrderManagementByNumber(gSaleOrderManagement);
        return ResultBody.success();
    }

}
